<template>
    <div class="f-menu" :style="{ width:$store.state.asideWidth }">
        <el-menu :default-active="defaultActive" unique-opened :collapse="isCollapse"  class="border-0" @select="handleSelect" :collapse-transition="false">
            <template v-for="(item,index) in asideMenus" :key="index">
                <el-sub-menu v-if="item.children && item.children.length > 0" :index="item.route">
                    <template #title>
                        <el-icon>
                            <component :is="item.icon"></component>
                        </el-icon>
                        <span class="ml-1 text-base">{{ item.title }}</span>
                    </template>
                    <el-menu-item v-for="(item2,index2) in item.children" :key="index2" :index="item2.route">
                        <span >{{ item2.title }}</span>
                    </el-menu-item>
                </el-sub-menu>

                <el-menu-item v-else :index="item.route">
                    <el-icon>
                         <component :is="item.icon"></component>
                    </el-icon>
                    <span class="ml-1 text-base">{{ item.title }}</span>
                </el-menu-item>
            </template>
        </el-menu>
    </div>
</template>
<script setup>
import { computed,ref,watchEffect } from 'vue';
import { useRouter,useRoute,onBeforeRouteUpdate } from 'vue-router';
import { useStore } from 'vuex';
const router = useRouter()
const store = useStore()
const route = useRoute()


// 默认选中
const defaultActive = ref(route.path)
// 监听路由变化
onBeforeRouteUpdate((to,from)=>{
    const routeValue = activeRoute(to.path);
    if(routeValue == 1){
        defaultActive.value = to.path
    }else{
        defaultActive.value = routeValue;
    }
})

// 是否折叠
const isCollapse = computed(()=> !(store.state.asideWidth == '250px'))
//获取用户菜单
const asideMenus = computed(()=>store.state.menus)
// 获取数据
function activeRoute(currentPath) {
    let activeValue = 1;
    let menuList = asideMenus.value || [];
    let route_name = currentPath;
    if (menuList.length > 0) {
        for (let i = 0; i < menuList.length; i++) {
            let currentMenu = menuList[i];
            if (currentMenu.children) {
                (currentMenu.children || []).every((item_c) => {
                    if (item_c.route === route_name) { //eg /unit/index
                        activeValue = 1;
                    } else {
                        let data_cc = item_c.children || [];
                        data_cc.every((item_cc) => {
                            if (item_cc.route === route_name) {
                                activeValue = item_c.route;
                            }
                            return true;
                        });
                    }
                    return true;
                });
            }
        }
    }
   return activeValue;
}

const handleSelect = (e)=>{
    router.push(e)
}

watchEffect(()=>{
    const routeValue = activeRoute(route.path);
    if(routeValue == 1){
        defaultActive.value = route.path
    }else{
        defaultActive.value = routeValue;
    }
})
</script>
<style>
.f-menu {
    transition: all 0.2s;
    top: 64px;
    bottom: 0;
    left: 0;
    overflow-y: auto;
    overflow-x: hidden;
    @apply shadow-md fixed bg-light-50;
}
.f-menu::-webkit-scrollbar{
    width: 0;
}
</style>